package com.jwell.panprovider.tools;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;

/**
 * dimen转换工具，用于屏幕适配，本项目是以720*1280-360dpi-sw360为模板
 * <p>
 * sw表示最短分辨率
 * <p>
 * <p>
 * Copy by Mr.Feng
 *
 * @author 76577
 */
public class DimenTool {

    public static void gen() {
        float dpi = 360.00f;

        DecimalFormat decimalFormat = new DecimalFormat("0.0");
        File file = new File("./app/src/main/res/values/dimens.xml");

        BufferedReader reader = null;
        StringBuilder sw320 = new StringBuilder();
        StringBuilder sw360 = new StringBuilder();
        StringBuilder sw384 = new StringBuilder();
        StringBuilder sw400 = new StringBuilder();
        StringBuilder sw411 = new StringBuilder();
        StringBuilder sw533 = new StringBuilder();
        StringBuilder sw640 = new StringBuilder();
        StringBuilder sw720 = new StringBuilder();
        StringBuilder sw768 = new StringBuilder();
        StringBuilder sw820 = new StringBuilder();


        try {
            System.out.println("生成不同分辨率：");
            reader = new BufferedReader(new FileReader(file));
            String tempString;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {

                if (tempString.contains("</dimen>")) {
                    String start = tempString.substring(0, tempString.indexOf(">") + 1);
                    String end = tempString.substring(tempString.lastIndexOf("<") - 2);
                    float num = Float.valueOf(tempString.substring(tempString.indexOf(">") + 1, tempString.indexOf("</dimen>") - 2));
                    sw320.append(start).append(decimalFormat.format(num * 320.00f / dpi)).append(end).append("\n");
                    sw360.append(start).append(decimalFormat.format(num * 360.00f / dpi)).append(end).append("\n");
                    sw384.append(start).append(decimalFormat.format(num * 384.00f / dpi)).append(end).append("\n");
                    sw400.append(start).append(decimalFormat.format(num * 400.00f / dpi)).append(end).append("\n");
                    sw411.append(start).append(decimalFormat.format(num * 411.00f / dpi)).append(end).append("\n");
                    sw533.append(start).append(decimalFormat.format(num * 533.00f / dpi)).append(end).append("\n");
                    sw640.append(start).append(decimalFormat.format(num * 640.00f / dpi)).append(end).append("\n");
                    sw720.append(start).append(decimalFormat.format(num * 720.00f / dpi)).append(end).append("\n");
                    sw768.append(start).append(decimalFormat.format(num * 768.00f / dpi)).append(end).append("\n");
                    sw820.append(start).append(decimalFormat.format(num * 820.00f / dpi)).append(end).append("\n");
                } else {

                    sw320.append(tempString).append("\n");
                    sw360.append(tempString).append("\n");
                    sw384.append(tempString).append("\n");
                    sw400.append(tempString).append("\n");
                    sw411.append(tempString).append("\n");
                    sw533.append(tempString).append("\n");
                    sw640.append(tempString).append("\n");
                    sw720.append(tempString).append("\n");
                    sw768.append(tempString).append("\n");
                    sw820.append(tempString).append("\n");

                }
                line++;
            }
            reader.close();

            String sw320file = "./app/src/main/res/values-sw320dp/dimens.xml";
            String sw360file = "./app/src/main/res/values-sw360dp/dimens.xml";
            String sw384file = "./app/src/main/res/values-sw384dp/dimens.xml";
            String sw400file = "./app/src/main/res/values-sw400dp/dimens.xml";
            String sw411file = "./app/src/main/res/values-sw411dp/dimens.xml";
            String sw533file = "./app/src/main/res/values-sw533dp/dimens.xml";
            String sw640file = "./app/src/main/res/values-sw640dp/dimens.xml";
            String sw720file = "./app/src/main/res/values-sw720dp/dimens.xml";
            String sw768file = "./app/src/main/res/values-sw768dp/dimens.xml";
            String sw820file = "./app/src/main/res/values-sw820dp/dimens.xml";

            writeFile(sw320file, sw320.toString());
            writeFile(sw360file, sw360.toString());
            writeFile(sw384file, sw384.toString());
            writeFile(sw400file, sw400.toString());
            writeFile(sw411file, sw411.toString());
            writeFile(sw533file, sw533.toString());
            writeFile(sw640file, sw640.toString());
            writeFile(sw720file, sw720.toString());
            writeFile(sw768file, sw768.toString());
            writeFile(sw820file, sw820.toString());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    public static void writeFile(String filePath, String text) {

        PrintWriter out = null;
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                file.getParentFile().mkdirs();
                file.createNewFile();
            }
            out = new PrintWriter(new BufferedWriter(new FileWriter(filePath)));
            out.println(text);
        } catch (IOException e) {
            e.printStackTrace();
        }

        out.close();
    }

    public static void main(String[] args) {
//        init();//第一次调用
        gen();//第二次调用
    }

    public static void init() {
        File file = new File("./app/src/main/res/values/dimens.xml");
        try {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
            bufferedWriter.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            bufferedWriter.write("<resources>\n");
            for (int i = 1; i < 100; i++) {
                bufferedWriter.write("<dimen name=\"sp_" + i + "\">" + i + "sp</dimen>\n");

            }
            for (int i = 1; i < 1000; i++) {
                bufferedWriter.write(" <dimen name=\"dp_" + i + "\">" + i + "dp</dimen>\n");
            }
            bufferedWriter.write("</resources>\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
